package com.kun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.annotation.LoginUser;
import com.kun.entity.SysDept;
import com.kun.entity.SysRole;
import com.kun.entity.SysUser;
import com.kun.service.SysDeptService;
import com.kun.service.SysRoleService;
import com.kun.transform.TableData;
import com.kun.transform.TransformUtil;
import com.kun.util.ResponseResult;
import com.kun.vo.SysRolePermission;
import com.kun.dto.SysRoleDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kun.li
 */
@RestController
@RequestMapping("role")
@Tag(name = "角色管理", description = "角色管理")
public class RoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Autowired
    private SysDeptService sysDeptService;


    @GetMapping("getRoleList")
    public List<SysRole> getRoleList(String userId) {
        return sysRoleService.selectSysRole(userId);
    }


    @Operation(summary = "角色表格")
    @PostMapping("tableData")
    public ResponseResult<TableData> tableData(@RequestBody(required = false) SysRoleDTO sysRoleDTO) {
        if (sysRoleDTO.getPage() != null && sysRoleDTO.getSize() != null) {
            Page<SysRole> sysRolePage = sysRoleService.page(Page.of(sysRoleDTO.getPage(), sysRoleDTO.getSize()));
            return ResponseResult.success(TransformUtil.transformPage(sysRolePage));
        }
        List<SysRole> sysRoles = sysRoleService.list();
        return ResponseResult.success(new TableData(sysRoles));
    }

    @Operation(summary = "根据用户查询角色")
    @GetMapping("selectListRole/{userId}")
    public ResponseResult<List<SysRole>> selectListRole(@PathVariable String userId) {
        return ResponseResult.success(sysRoleService.selectSysRole(userId));
    }

    @GetMapping("view/{roleId}")
    @Operation(summary = "查看角色")
    public ResponseResult<SysRole> view(@PathVariable String roleId) {
        return ResponseResult.success(sysRoleService.getById(roleId));
    }


    @PostMapping("add")
    @Operation(summary = "角色新增")
    public ResponseResult<Boolean> save(@LoginUser(isFull = true) SysUser sysUser, @RequestBody SysRole sysRole) {
        commonDataScopeSet(sysUser, sysRole);
        return ResponseResult.success(sysRoleService.save(sysRole));
    }

    @PostMapping("delete")
    @Operation(summary = "角色删除")
    public ResponseResult<Boolean> delete(@RequestBody List<String> sysRole) {
        return ResponseResult.success(sysRoleService.removeBatchByIds(sysRole));
    }


    @Operation(summary = "给角色分配权限")
    @PostMapping("assignmentPermission")
    public ResponseResult<Boolean> assignmentPermission(@RequestBody SysRolePermission sysRolePermission) {
        return ResponseResult.success(sysRoleService.assignmentPermission(sysRolePermission));
    }


    @Operation(summary = "更新角色信息")
    @PostMapping("updateRoleInfo")
    public ResponseResult<Boolean> updateRoleInfo(@LoginUser(isFull = true) SysUser sysUser, @RequestBody SysRole sysRole) {
        // 当前登陆用户对应部门
        commonDataScopeSet(sysUser, sysRole);
        return ResponseResult.success(sysRoleService.updateById(sysRole));
    }

    private void commonDataScopeSet(SysUser sysUser, SysRole sysRole) {
        String deptId = sysUser.getDeptId();
        switch (sysRole.getDsType()) {
            case 1 -> sysRole.setDsScope(deptId);
            // 本部门以及子部门,
            case 2 -> {
                LambdaQueryWrapper<SysDept> queryWrapper = Wrappers.<SysDept>lambdaQuery()
                        .eq(SysDept::getParentId, deptId);
                List<SysDept> sysDeptList = sysDeptService.list(queryWrapper);
                String deptIds = sysDeptList.stream().map(SysDept::getDeptId).collect(Collectors.joining(","));
                sysRole.setDsScope(deptIds + "," + deptId);
            }
        }
    }


}
